package com.mycar2w.cloud.web.controller;

import com.mycar2w.cloud.entity.Car;
import com.mycar2w.cloud.service.serviceimpl.CommService;
import com.mycar2w.cloud.web.util.ReturnModelAndView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * created time 2017/3/25
 *
 * @author ljc
 * @version V1.0
 */
@RestController
@RequestMapping("/comm")
public class CommController extends MycarBaseConTroller{
    @Autowired
    private CommService commService;
    Map<String,Object> result=new HashMap<>();

    /**
     * 跳转的首页的页面上
     * @return
     */
    @RequestMapping(value = "/index")
    public ModelAndView pageIndex(){
        return ReturnModelAndView.returnSuccess("index");
    }
    /**
     * 无条件查询汽车列表
     * @param pageIndex 页码
     * @return 汽车集合 Page
     */
    @RequestMapping(value = "/index_get/{pageIndex}",method = RequestMethod.GET)
    public Map<String,Object> CommIndexGet(@PathVariable Integer pageIndex){
        List<Car>  cars=commService.findCarListByNone(pageIndex);
        result.put("cars",cars);
        return result;
    }

    /**
     * 获得价格区间的汽车列表
     * @param beginPrice 其实价格
     * @param endPrice 最终价格
     * @param pageIndex 页码
     * @param sort 排序规则
     * @return 汽车集合 Page
     */
    @RequestMapping(value = "/list_between_price/get/{beginPrice}/{endPrice}/{pageIndex}/{sort}")
    public Map<String,Object> CommCarListByBetweenPrice(@PathVariable String beginPrice,@PathVariable String endPrice,@PathVariable Integer pageIndex,@PathVariable Integer sort){
        List<Car> cars=commService.findCarListByBetweenPrice(beginPrice,endPrice,pageIndex,sort);
        result.put("cars",cars);
        return result;
    }

    /**
     * 获得某个地区的汽车资源
     * @param area 地区
     * @param pageIndex 页码
     * @param sort 排序规则
     * @return 汽车集合 Page
     */
    @RequestMapping(value = "list_area/{area}/{pageIndex}/{sort}",method = RequestMethod.GET)
    public Map<String,Object> CommCarListByArea(@PathVariable Long area,@PathVariable Integer pageIndex,@PathVariable Integer sort){
        List<Car> cars=new ArrayList<>();
        try {
            cars = commService.findCarListByArea(area, pageIndex, sort);
        }catch (Exception ex){
            result.put("cars",cars);
            return result;
        }
        result.put("cars",cars);
        return result;
    }

    /**
     * 该车是几次交易
     * @param count 交易次数
     * @param pageIndex 页码
     * @param sort 排序方式
     * @return
     */
    @RequestMapping(value = "/list_transfer_count/{count}/{pageIndex}/{sort}",method = RequestMethod.GET)
    public Map<String,Object> CommCarListByTransferCount(@PathVariable String count,@PathVariable Integer pageIndex,@PathVariable Integer sort){
        List<Car> cars=commService.findCarListByTransfercount(count,pageIndex,sort);
        result.put("cars",cars);
        return result;
    }

    /**
     * 通过名称进行模糊查询
     * @param carName car名字
     * @param pageIndex 页码
     * @return
     */
    @RequestMapping(value = "/list_car_name/{carName}/{pageIndex}")
    public Map<String,Object> CommCarFindByName(@PathVariable String carName,@PathVariable Integer pageIndex){
        List<Car> cars=commService.findCarListByName(carName,pageIndex);
        result.put("cars",cars);
        return result;
    }
}
